<?php

namespace app;

use epii\admin\center\libs\Tools;
use epii\server\Args;
use epii\ui\upload\AdminUiUpload;
use epii\ui\upload\driver\LocalFileUploader;
use think\Db;

class res extends base
{
    private $pre = 'https://cyz.oss-cn-north-2.unicloudsrv.com/';
    public function index(){
        $this->assign('opts',$this->getOptions(null,['id'=>0,'name'=>'--请选择--']));
        $this->adminUiDisplay();
    }
    public function ajaxdata(){
        $map = [];

        if ($name = Args::params("name/s")) {
            $map[] = ["name", "LIKE", "%{$name}%"];
        }
        if ($path = Args::params("path/s")) {
            $map[] = ["path", "LIKE", "%{$path}%"];
        }
        if($belong = Args::params('belong')){
            $map[] = ["belong", "=", $belong];
        }

        return $this->tableJsonData('resource', $map, function ($row) {
            $row['path'] = $this->pre.$row['path'];

            $preview = <<<HTML
<a class="btn-dialog" data-intop="1" data-area="70%,70%" title="预览" href="?app=res@preview&id={$row['id']}">
<img height="100" width="100" src="{$row['path']}" />
</a>
HTML;

            $row['preview'] = $preview;
            $row['belong'] = $this->getOptions($row['belong']);

            return $row;
        });
    }
    public function add(){
        $id = Args::params('id/d');
        if(Args::isPost()){
            $path = str_replace('\\','/',Args::params('path/1','资源路径不能为空'));
            $path = str_replace($this->pre,'',$path);
            $belong = Args::params('belong/d/1');
            $code = Args::params('code/s/1','资源编码不能为空');
            // 检测编码是否重复
            $existid = Db::name('resource')->where('code',$code)->value('id');

            if($existid){
                if(!$id ||  $existid!=$id){
                    $this->error('资源编码不能重复');
                }
            }
            $data = [
                'name'=>Args::params('name/1','资源名称不能为空'),
                'code'=>Args::params('code/s/1','资源编码不能为空'),
                'path'=>$path,
                'belong'=>$belong
            ];
            if($id){
                Db::name('resource')->where('id',$id)->update($data);
            }else{
                Db::name('resource')->insertGetId($data);
            }
            $this->success();
        }else{
            $info=[];
            if($id){
                $info = Db::name('resource')->where('id',$id)->find();
                $info['path'] = $this->pre.$info['path'];
            }
            $this->assign('opts',$this->getOptions(null,['id'=>0,'name'=>'--请选择--']));
            $this->assign('info',$info);
            $this->adminUiDisplay();
        }
    }
    public function del(){
        $id = Args::params('id/d/1');
        Db::name('resource')->where('id',$id)->delete();
        $this->success('成功','refresh');
    }

//    public function doUpload(){
//        AdminUiUpload::setUploadHandler(LocalFileUploader::class);
//        // url_pre 其实没用
//        LocalFileUploader::init(__DIR__.'/../public/res',\libs\Tools::getResPre());
//        $res = AdminUiUpload::doUpload(["jpg","png",'gif','jpeg'], 1024000000);
//        echo $res;
//        exit;
//    }

    public function preview(){
        $id = Args::params('id/d/1');
        $path = Db::name('resource')->where('id',$id)->value('path');
        $this->assign('path',$path);
        $this->adminUiDisplay();
    }

    private function getOptions($id=null,$unshiftopt=[]){
        $list = [
            ['id'=>1,'name'=>'患者端'],
            ['id'=>2,'name'=>'医师端']
        ];
        if($unshiftopt){
            array_unshift($list,$unshiftopt);
        }
        if($id==null){
            return $list;
        }else{
            $res = '';
            $arr = array_column($list,'name','id');
            if(isset($arr[$id])){
                $res = $arr[$id];
            }
            return $res;
        }
    }
}